package top.kaoshanji.ch06;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.format.TextStyle;
import java.util.Locale;

/**
 * 代码清单 ch06-04：格式化和解析示例(6.6节)
 * author kaoshanji
 * ^_^
 * create time 2024/12/9 16:15
 */
public class Formatting {

    private static final Logger logger = LogManager.getLogger(Formatting.class);

    public static void main(String[] args) {
        logger.info("...代码清单 ch06-04：格式化和解析示例(6.6节)...");

        ZonedDateTime apollo11launch = ZonedDateTime.of(1969, 7, 16, 9, 32, 0, 0,
                ZoneId.of("America/New_York"));

        logger.info("...预定义的格式器...");
        String formatted = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(apollo11launch);
        System.out.println(formatted);

        logger.info("...locale相关的格式器...");
        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        formatted = formatter.format(apollo11launch);
        System.out.println(formatted);
        formatted = formatter.withLocale(Locale.FRENCH).format(apollo11launch);
        System.out.println(formatted);

        logger.info("...自定义的格式器...");
        formatter = DateTimeFormatter.ofPattern("E yyyy-MM-dd HH:mm");
        formatted = formatter.format(apollo11launch);
        System.out.println(formatted);

        logger.info("...解析字符串中的日期/时间...");
        LocalDate churchsBirthday = LocalDate.parse("1903-06-14");
        System.out.println("churchsBirthday: " + churchsBirthday);
        apollo11launch = ZonedDateTime.parse("1969-07-16 03:32:00-0400", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssxx"));
        System.out.println("apollo11launch: " + apollo11launch);

        logger.info("...locale和格式给出星期日期...");
        for (DayOfWeek w: DayOfWeek.values()) {
            System.out.println(w.getDisplayName(TextStyle.SHORT, Locale.ENGLISH) + " ");
        }

    }

}
